// pages/admin/adminhome/adminhome.js
import sendRequest from '../../rpromise/request'

Page({

    /**
     * 页面的初始数据
     */
    data: {
        home: true,
        my: false,
        headimg: '/pages/images/user.png',
        username: '管理员',
        huodon: [{
                stype: '活动',
                count: 0,
                src: '/pages/images/admin/leiji.png'
            },
            {
                stype: '校级',
                count: 0,
                src: '/pages/images/admin/xiaoji.png'
            },
            {
                stype: '院级',
                count: 0,
                src: '/pages/images/admin/xueyuan.png'
            },
        ],
        funtable: [{
                id: 1,
                lable: '活动审核',
                img: '/pages/images/admin/shenhe_red.png'
            },
            {
                id: 3,
                lable: '账号管理',
                img: '/pages/images/admin/zhanghu.png'
            },
            {
                id: 2,
                lable: '活动统计',
                img: '/pages/images/admin/zhanghu.png'
            },
            {
                id: 5,
                lable: '',
                img: ''
            }
        ],
        myusedata: [{
                id: 1,
                label: '个人信息',
                img: '/pages/images/teacher/geren.png'
            },
            {
                id: 2,
                label: '使用说明',
                img: '/pages/images/shuoming.png'
            },
            {
                id: 3,
                label: '设置中心',
                img: '/pages/images/shezhi.png'
            }
        ],
        activity:[],
        tdata:[],
        adminno:'',
        admindata:{},

    },
    homeBar() {
        this.setData({
            home: true,
            my: false
        })
    },
    getHead(e) {
        var username = '微信用户'
        var headimg = e.detail.avatarUrl
        wx.setStorageSync('nickName', username)
        wx.setStorageSync('avatarUrl', headimg)
        this.setData({
            username: username,
            headimg: headimg
        })
        
    },
    myBar() {
        this.setData({
            home: false,
            my: true
        })

    },
    mysetBtn(e) {
        // console.log(e.currentTarget.dataset.title)
        var title = e.currentTarget.dataset.title
        var signinfo = JSON.stringify(this.data.admindata)
        var url = ''
        if (title === '设置中心') {
            url = '/pages/admin/adminsite/adminsite?title=' + title + '&signinfo=' + signinfo
        } else if (title === '个人信息') {
            url = '/pages/admin/adminedit/adminedit?title=' + title + '&signinfo=' + signinfo
        } else {
            url = '/pages/admin/adminuseinfo/adminuseinfo?title=' + title + '&signinfo=' + signinfo
        }
        wx.navigateTo({
            url: url,
        })
    },
    Display(e) {
        // console.log(e.currentTarget.dataset)
        var order = e.currentTarget.dataset.order
        var title = e.currentTarget.dataset.title
        var activity = JSON.stringify(this.data.activity)
        if(order===3){
            activity=JSON.stringify(this.data.tdata)
        }else if(order===2){
            wx.navigateTo({
                url: '/pages/admin/adminjoindata/adminjoindata?title=' + title + '&activity='+activity,
            })
            return
        }
        wx.navigateTo({
            url: '/pages/admin/adminplat/adminplat?order=' + order + '&title=' + title + '&activity='+activity,
        })
    },
    async loadDatas() {
        var that = this
        var sql = "SELECT * FROM activity"
       
        this.sqlRequest(sql).then(res => {
            console.log(res.data)
            that.setData({
                activity:res.data
            })
        })
        var sql = "SELECT * FROM admin WHERE ano='"+this.data.adminno+"'"
       
        this.sqlRequest(sql).then(res => {
            console.log(res.data)
            that.setData({
                admindata:res.data[0]
            })
        })
        sql ="SELECT * FROM teacher WHERE identity='专用'"
        this.sqlRequest(sql).then(res => {
            console.log(res.data)
            that.setData({
                tdata:res.data
            })
        })
        var sql = "SELECT stype, COUNT(*) as count FROM activity GROUP BY stype;"
        this.sqlRequest(sql).then(res=>{
            console.log(res.data)
            var adata = that.data.huodon
            var count = 0
            res.data.forEach(item => {
                if(item.stype==='校级'){
                    adata[1].count=item.count
                }else{
                    adata[2].count=item.count
                }
                count = count+item.count
                
            });
            adata[0].count=count
           
            this.setData({
                huodon:adata
            })
        })
        // console.log(res1.data)
    },
    
    countBtn(e) {
        console.log(e)
        var identity = e.currentTarget.dataset.identity
        var data = []
        if(identity==='活动'){
            data=this.data.activity
        }else{
            this.data.activity.forEach(item => {
                if(item.stype===identity){
                    data.push(item)
                }
            });
        }
        var url = '/pages/admin/admincount/admincount?identity=' + identity + '&data=' + JSON.stringify(data)
        // console.log(identity)
        wx.navigateTo({
            url: url,
        })
    },
    
    async sqlRequest(sql) {
        const res1 = await sendRequest({
            method: 'POST',
            url: '/manage',
            data: {
                sql: sql
            },
        })
        // console.log(res1)
        return res1
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.setData({
            adminno:wx.getStorageSync('admin').user
        })
        console.log(this.data.admindata)
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {
        var username = wx.getStorageSync('nickName')
        var headimg = wx.getStorageSync('avatarUrl')
        if (username) {
            this.setData({
                username: username,
                headimg: headimg
            })
        }
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        this.loadDatas()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})